
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 07/24/2018 10:37:05
-- Generated from EDMX file: H:\ZSZDemo\TestService\ModelTest.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [ModeTestDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------


-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------


-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'AdminUserSet'
CREATE TABLE [dbo].[AdminUserSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'RoleSet'
CREATE TABLE [dbo].[RoleSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'PermissionSet'
CREATE TABLE [dbo].[PermissionSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'AdminUserRole'
CREATE TABLE [dbo].[AdminUserRole] (
    [AdminUser_Id] int  NOT NULL,
    [Role_Id] int  NOT NULL
);
GO

-- Creating table 'PermissionRole'
CREATE TABLE [dbo].[PermissionRole] (
    [Permission_Id] int  NOT NULL,
    [Role_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'AdminUserSet'
ALTER TABLE [dbo].[AdminUserSet]
ADD CONSTRAINT [PK_AdminUserSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RoleSet'
ALTER TABLE [dbo].[RoleSet]
ADD CONSTRAINT [PK_RoleSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PermissionSet'
ALTER TABLE [dbo].[PermissionSet]
ADD CONSTRAINT [PK_PermissionSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [AdminUser_Id], [Role_Id] in table 'AdminUserRole'
ALTER TABLE [dbo].[AdminUserRole]
ADD CONSTRAINT [PK_AdminUserRole]
    PRIMARY KEY CLUSTERED ([AdminUser_Id], [Role_Id] ASC);
GO

-- Creating primary key on [Permission_Id], [Role_Id] in table 'PermissionRole'
ALTER TABLE [dbo].[PermissionRole]
ADD CONSTRAINT [PK_PermissionRole]
    PRIMARY KEY CLUSTERED ([Permission_Id], [Role_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [AdminUser_Id] in table 'AdminUserRole'
ALTER TABLE [dbo].[AdminUserRole]
ADD CONSTRAINT [FK_AdminUserRole_AdminUser]
    FOREIGN KEY ([AdminUser_Id])
    REFERENCES [dbo].[AdminUserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Role_Id] in table 'AdminUserRole'
ALTER TABLE [dbo].[AdminUserRole]
ADD CONSTRAINT [FK_AdminUserRole_Role]
    FOREIGN KEY ([Role_Id])
    REFERENCES [dbo].[RoleSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_AdminUserRole_Role'
CREATE INDEX [IX_FK_AdminUserRole_Role]
ON [dbo].[AdminUserRole]
    ([Role_Id]);
GO

-- Creating foreign key on [Permission_Id] in table 'PermissionRole'
ALTER TABLE [dbo].[PermissionRole]
ADD CONSTRAINT [FK_PermissionRole_Permission]
    FOREIGN KEY ([Permission_Id])
    REFERENCES [dbo].[PermissionSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Role_Id] in table 'PermissionRole'
ALTER TABLE [dbo].[PermissionRole]
ADD CONSTRAINT [FK_PermissionRole_Role]
    FOREIGN KEY ([Role_Id])
    REFERENCES [dbo].[RoleSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_PermissionRole_Role'
CREATE INDEX [IX_FK_PermissionRole_Role]
ON [dbo].[PermissionRole]
    ([Role_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------